//
//  BuddyListSource.m
//  HiQ
//
//  Created by shoguncao on 12-7-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MacrosDefine.h"
#import "Buddy.h"
#import "BuddyGroup.h"
#import "BuddyListSource.h"
#import "Connection.h"
#import "ImageAndTextCell.h"


@implementation BuddyListSource

@synthesize buddyGroupArray;

// Data source method

- (id)init
{
    buddyGroupArray = [[NSMutableArray alloc] init];
    
    return [super init];
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    if(item == nil)
        item = buddyGroupArray;
    
    if(item == buddyGroupArray)
        return [item objectAtIndex:index];
    
    if([item isKindOfClass:[BuddyGroup class]])
        return ((Buddy*)[((BuddyGroup *)item)->buddyList objectAtIndex:index]);
    
    return nil;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    if(item == nil)
        return NO;
    
    if([item isKindOfClass:[BuddyGroup class]])
        return YES;
    
    return NO;
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    if(item == nil)
        item = buddyGroupArray;
    
    if(item == buddyGroupArray)
        return [item count];
    
    if([item isKindOfClass:[BuddyGroup class]])
        return [((BuddyGroup *)item)->buddyList count];
    
    return 0;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
    if ([[tableColumn identifier] isEqualToString: @"BuddyList"])
	{
        if(item == nil)
            return nil;
        
        if([item isKindOfClass:[BuddyGroup class]])
            return ((BuddyGroup *)item)->groupName;
        
        //    if([item isKindOfClass:[Buddy class]])
        //        return ((Buddy*)item)->buddyName;
    }
    return nil;
}



@end
